package com.ruoyi.network.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.network.common.validation.UpdateGroupValidation;
import com.ruoyi.network.model.vo.IpAddressCountVO;
import com.ruoyi.network.model.vo.IpAddressPageVO;
import com.ruoyi.network.model.vo.IpAddressRequestVO;
import com.ruoyi.network.service.IIpAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * IP地址维护结构Controller
 *
 * @author lyp
 * @date 2024-12-13
 */
@RestController
@RequestMapping("/ip/address")
@Api(tags = "IP地址维护结构")
public class IpAddressController extends BaseController {
    @Resource
    private IIpAddressService ipAddressService;

    /**
     * 查询IP地址维护结构列表
     */
    @ApiOperation(value = "查询IP地址维护结构列表", response = TableDataInfo.class)
    @GetMapping("/list")
    public AjaxResult list(IpAddressPageVO vo) {
        return AjaxResult.success(ipAddressService.selectIpAddressList(vo));
    }

    /**
     * 导入IP地址数据
     */
    @Log(title = "导入IP地址数据", businessType = BusinessType.IMPORT)
    @ApiOperation(value = "导入IP地址数据")
    @PostMapping("/import")
    public AjaxResult importExcel(@RequestParam MultipartFile file) {
        return toAjax(ipAddressService.importExcel(file));
    }

    /**
     * 导出IP地址维护结构列表
     */
    @Log(title = "IP地址维护结构", businessType = BusinessType.EXPORT)
    @ApiOperation(value = "导出IP地址维护结构列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, IpAddressPageVO vo) {
        try {
            ipAddressService.export(response, vo);
        } catch (Exception e) {
            throw new ServiceException("数据导出失败!");
        }
    }

    /**
     * 获取IP地址维护结构详细信息
     */
    @ApiOperation(value = "获取IP地址维护结构详细信息", response = AjaxResult.class)
    @GetMapping(value = "/getId")
    public AjaxResult getInfo(Long id) {
        return success(ipAddressService.selectIpAddressById(id));
    }
    /**
     * 获取IP地址维护结构详细信息
     */
    @ApiOperation(value = "获取IP地址维护结构详细信息", response = AjaxResult.class)
    @GetMapping(value = "/getByRangeId")
    public AjaxResult getByRangeIdInfo(Long rangeId) {
        return success(ipAddressService.getByRangeIdInfo(rangeId));
    }

    /**
     * 新增IP地址维护结构
     */
    @Log(title = "IP地址维护结构", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增IP地址维护结构", response = AjaxResult.class)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody IpAddressRequestVO vo) {
        return toAjax(ipAddressService.insertIpAddress(vo));
    }

    /**
     * 修改IP地址维护结构
     */
    @Log(title = "IP地址维护结构", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改IP地址维护结构", response = AjaxResult.class)
    @PutMapping("/edit")
    public AjaxResult edit(@Validated({UpdateGroupValidation.class}) @RequestBody IpAddressRequestVO vo) {
        return toAjax(ipAddressService.updateIpAddress(vo));
    }

    /**
     * 删除IP地址维护结构
     */
    @Log(title = "IP地址维护结构", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除IP地址维护结构", response = AjaxResult.class)
    @DeleteMapping("/delete")
    public AjaxResult remove(@RequestParam(value = "ids") List<Long> ids) {
        return toAjax(ipAddressService.deleteIpAddressByIds(ids));
    }

    /**
     * IP地址统计
     */
    @ApiOperation(value = "IP地址统计", response = AjaxResult.class)
    @GetMapping("/count")
    public AjaxResult count(IpAddressCountVO vo) {
        return success(ipAddressService.count(vo));
    }
}
